package org.example.medical.service.impl;

import org.example.medical.Mapper.DiagnosisRecordMapper;
import org.example.medical.Mapper.DiseaseMapper;
import org.example.medical.entity.Disease;
import org.example.medical.service.DiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiseaseServiceImpl implements DiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private DiagnosisRecordMapper diagnosisRecordMapper;
    @Override
    public List<Disease> getDiseases(String name, String category, int page, int size) {
        int offset = (page - 1) * size;
        return diseaseMapper.selectByPage(name, category, offset, size);
    }

    @Override
    public void addDisease(Disease disease) {
        diseaseMapper.insertDisease(disease);

    }

    @Override
    public void updateDisease(Disease disease) {
        diseaseMapper.updateDisease(disease);

    }

    @Override
    public void softDeleteDisease(Integer diseaseId) {
        List<Long> diagnosis = diagnosisRecordMapper.getDiagnosisIdsByDiseaseId(diseaseId);
        if (diagnosis != null && !diagnosis.isEmpty()){
            diagnosisRecordMapper.batchSoftDeleteDiagnosisRecords(diagnosis);
        }
        diseaseMapper.softDeleteDisease(diseaseId);

    }

    @Override
    public void batchSoftDeleteDiseases(List<Integer> diseaseIds) {
        for(Integer diseaseId : diseaseIds){
            List<Long> diagnosis = diagnosisRecordMapper.getDiagnosisIdsByDiseaseId(diseaseId);
            if (diagnosis != null && !diagnosis.isEmpty()){
                diagnosisRecordMapper.batchSoftDeleteDiagnosisRecords(diagnosis);
            }
        }
        diseaseMapper.batchSoftDeleteDiseases(diseaseIds);
    }
}
